﻿#region Copyright RenGuiYou. All rights reserved.
//=====================================================
// NeatlyFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

public class PacketReader
{
    private const byte FALSE = 0;
    private const byte TRUE = 1;
    private byte[] data;
    private int offset;

    public byte[] GetData()
    {
        return data;
    }

    public byte[] GetLastData()
    {
        int sourceIndex = offset;
        byte[] lastData = new byte[data.Length - sourceIndex];
        Array.Copy(data, sourceIndex, lastData, 0, lastData.Length);
        return lastData;
    }

    public void InitBytes(byte[] bytes)
    {
        data = bytes;
        offset = 0;
    }

    public byte ReadByte()
    {
        byte result = data[offset];
        offset++;
        return result;
    }

    public short ReadShort()
    {
        short result = (short)(data[offset] << 8 | data[offset + 1]);
        offset += 2;
        return result;
    }

    public int ReadUShort()
    {
        int result = (data[offset] << 8 | data[offset + 1]);
        offset += 2;
        return result;
    }

    public int ReadInt()
    {
        int result = data[offset] << 24 | data[offset + 1] << 16 | data[offset + 2] << 8 | data[offset + 3];
        offset += 4;
        return result;
    }

    public uint ReadUInt()
    {
        uint result = (uint)data[offset] << 24 | (uint)data[offset + 1] << 16 | (uint)data[offset + 2] << 8 | data[offset + 3];
        offset += 4;
        return result;
    }

    //    public unsafe float ReadFloat()
    //    {
    //        uint v = (uint)(data[offset] << 24 | data[offset + 1] << 16 | data[offset + 2] << 8 | data[offset + 3]);
    //        return *(float*)v;
    //    }

    public float ReadFloat()
    {
        return ReadInt() / 1000.0f;
    }

    public long ReadLong()
    {
        long result =
            ((long)data[offset] << 56)
            | ((long)data[offset + 1] << 48)
            | ((long)data[offset + 2] << 40)
            | ((long)data[offset + 3] << 32)
            | ((long)data[offset + 4] << 24)
            | ((long)data[offset + 5] << 16)
            | ((long)data[offset + 6] << 8)
            | ((long)data[offset + 7]);
        offset += 8;
        return result;
    }

    public string ReadString()
    {
        int num = ReadInt();
        string str = Encoding.UTF8.GetString(data, offset, num);
        offset += num;
        return str;
    }

    public void ReadListLong(ref List<long> list)
    {
        int count = ReadInt();
        for (int i = 0; i < count; i++)
        {
            var l = ReadLong();
            list.Add(l);
        }
    }

    public void ReadBytes(ref byte[] array)
    {
        int count = ReadInt();
        for (int i = 0; i < count; i++)
        {
            var b = ReadByte();
            array[i] = b;
        }
    }

    public Vector3 ReadVector3()
    {
        Vector3 result = new Vector3(ReadFloat(), ReadFloat(), ReadFloat());
        return result;
    }

    public bool ReadBool()
    {
        byte val = ReadByte();
        return val == TRUE;
    }

    public Quaternion ReadQuaternion()
    {
        Quaternion result = new Quaternion(ReadFloat(), ReadFloat(), ReadFloat(), ReadFloat());
        return result;
    }
}